package com.btpj.module_tree_details.ui

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.paging.PagedListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.launcher.ARouter
import com.btpj.jetpack.base.BaseListActivity
import com.btpj.jetpack.entity.DataBean
import com.btpj.module_tree_details.adapter.TreeDetailListAdapter
import com.scwang.smartrefresh.layout.api.RefreshLayout

class TreeDetailActivity : BaseListActivity<DataBean, TreeDetailViewModel>() {

    @Autowired
    @JvmField
    var treeId = 0

    @Autowired
    lateinit var title: String

    companion object {
        fun newIntent(context: Context, treeId: Int, title: String): Intent {
            return Intent(context, TreeDetailActivity::class.java).apply {
                putExtra("treeId", treeId)
                putExtra("title", title)
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        ARouter.getInstance().inject(this)
        super.onCreate(savedInstanceState)
        mViewModel.treeId = treeId
    }

    override fun generateAdapter(): PagedListAdapter<DataBean, RecyclerView.ViewHolder> {
        return TreeDetailListAdapter(this) as PagedListAdapter<DataBean, RecyclerView.ViewHolder>
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        mViewModel.mDataSource?.invalidate()
        finishRefresh()
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        finishRefresh()
    }

    override fun initActionBar() {
        mBinding.tvTitle.text = title
        mBinding.ivBack.setOnClickListener { finish() }
    }

}